package com.example.fresh_fruit.service.Impl;

import com.example.fresh_fruit.entity.Commodity;
import com.example.fresh_fruit.mapper.CommodityMapper;
import com.example.fresh_fruit.service.CommodityService;
import com.example.fresh_fruit.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author :飞天の鱼
 * @date : 2022/8/15 15:02
 */
@Service
public class CommodityServiceImpl implements CommodityService {

    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public int checkCommodity(Integer id) {
        Commodity commodity = commodityMapper.findById(id);
        if(commodity==null){
            Result.fail("商品不存在");
            return 1;
        }
        if (commodity.getState() != 0) {
            Result.fail("商品的状态不对");
            return 2;
        } else if (commodity.getIsdelete() != 0) {
            Result.fail("商品不存在");
            return 3;
//        } else if (commodity.getNumber() < orderDatil.getPrice()) {
//            Result.fail("商品数量不足");
//            return 3;

        }
        return 0;
    }


    @Override
    public boolean insertCom(Commodity commodity) {
        return commodityMapper.insert(commodity);
    }

    @Override
    public Commodity findName(String name) {
        return commodityMapper.findName(name);
    }

    @Override
    public boolean upCom(Commodity commodity) {
        return commodityMapper.upCommodity(commodity);
    }

    @Override
    public Commodity isdelete(String name) {
        return commodityMapper.isdelete(name);
    }

    @Override
    public List<Commodity> findByName(String name) {
        return commodityMapper.findByName(name);
    }

    @Override
    public Commodity findById(Integer id) {
        Commodity commodity = commodityMapper.findById(id);
        if (commodity == null) {
            return null;
//return Result.error().message("\"获取商品详情失败！尝试访问的商品数据不存在");
        }
        return commodity;
    }

    @Override
    public boolean updata(Commodity commodity) {//修改商品的信息
        return commodityMapper.updata(commodity);
    }

    @Override
    public boolean updataNumber(Commodity commodity) {
        return commodityMapper.updataNumber(commodity);
    }


}
